package lukeer.fencing;

import csproj.fencing.Move;
import csproj.fencing.Player;

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;

/**
 * Player using user input to generate a move object.
 *
 * User: Luke Richardson lukeer, Wafa M'Barek wmbarek
 * Date: 15/09/12
 * Time: 20:32
 */
public class TestingPlayer extends BasicPlayer implements Player, csproj.fencing.Piece {
    private final BufferedReader reader = new BufferedReader(new InputStreamReader(System.in));

    /**
     * Requests a piece from the user and places the move on the currentGameState board.
     *
     * @return move to be tested by opponent
     */
    public Move makeMove() {
        Move m;
        while (true) {
            try {
                m = getMoveFromSTDIN();
                break;
            } catch (IOException e) {
                System.err.println("Problem with move");
            }
        }

        try {
            currentGameState.placePiece(m);
        } catch (FencingException e) {
            System.err.println("Illegal move: " + e.getMessage());
            System.exit(1);
        }
        return m;
    }

    /**
     * Generates random moves, checks against arrays which keep track of board state.
     *
     * @return move object
     */
    private Move getMoveFromSTDIN() throws IOException {
        int x, y, fX, fY, direction;
        Pair loc;
        int userChoice = 0;

        if (currentGameState.isFull()) { // Board is full
            System.out.println("Fence only");
            userChoice = 3;
        } else {
            System.out.println("1. BoardPiece");
            System.out.println("2. BoardPiece and a fence");

            try {
                System.out.print("Selection: ");
                userChoice = Integer.parseInt(reader.readLine());
            } catch (Exception e) {
                System.err.println("Please enter an integer");
            }
        }
        if (userChoice == 1) {
            System.out.println("BoardPiece");
            loc = requestPosition();
            x = loc.getX();
            y = loc.getY();
            return new Move(x, y, player, true, -1, -1, false, 0);

        }

        if (userChoice == 3) {
            System.out.println("Fence");
            loc = requestPosition();
            x = loc.getX();
            y = loc.getY();
            direction = requestDirection();
            return new Move(-1, -1, player, true, x, y, true, direction);

        } else {
            System.out.println("BoardPiece");
            loc = requestPosition();
            x = loc.getX();
            y = loc.getY();
            System.out.println("Fence");
            loc = requestPosition();
            fX = loc.getX();
            fY = loc.getY();
            direction = requestDirection();
            return new Move(x, y, player, true, fX, fY, true, direction);
        }
    }

    /**
     * Request position from user
     *
     * @return pair object containing user input
     * @throws IOException if input is not an int
     */
    private Pair requestPosition() throws IOException {
        System.out.print("\nx: ");
        int x = Integer.parseInt(reader.readLine());
        System.out.print("\ny: ");
        int y = Integer.parseInt(reader.readLine());
        return new Pair(x, y);
    }

    /**
     * Request direction from user
     *
     * @return return direction as integer either HORIZONTAL or VERTICAL
     * @throws IOException if input is not an int
     */
    private int requestDirection() throws IOException {
        System.out.println("1. Horizontal, 2. Vertical");
        System.out.print("Direction: ");
        int selection = Integer.parseInt(reader.readLine());
        switch (selection) {
            case HORIZONTAL:
                return HORIZONTAL;
            case VERTICAL:
                return VERTICAL;
        }
        throw new IOException();
    }
}
